﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BusAppCore.Util.NativeExt;
using TableTool.Model;
using static Dm.net.buffer.ByteArrayBuffer;

namespace TableTool
{
    /// <summary>
    /// 提示窗体
    /// </summary>
    public partial class AlarmFrm : Form
    {
        /// <summary>
        /// 
        /// </summary>
        public LookModel lookModel { get; set; }
        private bool MouseFlag { get; set; }
        private Point mouse { get; set; }
        StringFormat stringFormat { get; set; }
        SolidBrush brush { get; set; }
        public DateTime LastTime { get; set; }
        public string keystr { get; set; }

        /// <summary>
        /// 是否就绪
        /// </summary>
        public bool IsOk { get; set; }
        public AlarmFrm()
        {
            InitializeComponent();
            DoubleBuffered = true;
            this.Paint += AlarmFrm_Paint;
            this.MouseDown += AlarmFrm_MouseDown;
            this.MouseMove += AlarmFrm_MouseMove;
            this.MouseUp += AlarmFrm_MouseUp;
            this.Load += AlarmFrm_Load;
            mouse = new Point(0, 0);
            stringFormat = new StringFormat()
            {
                Alignment = StringAlignment.Center,
                LineAlignment = StringAlignment.Center,
            };
            brush = new SolidBrush(Color.Black);
            LastTime = DateTime.Now;
        }

        private void AlarmFrm_Load(object? sender, EventArgs e)
        {
            ShowInTaskbar = false;
            if (lookModel != null)
            {
                lookModel.Keys.ForEach(key =>
                {
                    keystr += key.ToString() + " ";
                });
                if (lookModel.locationX > 0 && lookModel.locationY > 0)
                {
                    this.Location = new Point(lookModel.locationX, lookModel.locationY);
                }
                if (lookModel.width > 0)
                {
                    this.Width = lookModel.width;
                }
                if (lookModel.height > 0)
                    this.Height = lookModel.height;
            }
        }

        private void AlarmFrm_MouseUp(object? sender, MouseEventArgs e)
        {
            MouseFlag = false;
            var item = ElsTool.usdata.dataItems.Find(x => x.guid == lookModel.guid);
            if (item != null)
            {
                item.locationX = this.Location.X;
                item.locationY = this.Location.Y;
            }
        }

        private void AlarmFrm_MouseMove(object? sender, MouseEventArgs e)
        {
            if (MouseFlag)
            {
                this.Location = new Point(MousePosition.X - mouse.X,
                   MousePosition.Y - mouse.Y);
            }
        }

        private void AlarmFrm_MouseDown(object? sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouse = new Point(e.X, e.Y);
                MouseFlag = true;
            }
        }

        private void AlarmFrm_Paint(object? sender, PaintEventArgs e)
        {
            if (lookModel != null)
            {
                string text = "";
                var sp = DateTime.Now - LastTime;
                brush.Color = Color.White;
                if (sp > TimeSpan.FromSeconds(lookModel.timeSecond))
                {
                    e.Graphics.Clear(Color.Green);
                    text += string.Format("{0} {1} {2}", lookModel.Name, lookModel.timeSecond, "就绪");
                    IsOk = true;
                }
                else
                {
                    e.Graphics.Clear(Color.Red);
                    text += string.Format("{0} {1} {2}", lookModel.Name, lookModel.timeSecond, (lookModel.timeSecond - sp.Seconds).ToString());
                    IsOk = false;
                }
                var size = TextRenderer.MeasureText(text, this.Font);
                e.Graphics.DrawString(text, this.Font, brush, new RectangleF(0, 0, this.Width, this.Height), stringFormat);
            }
        }

        public void Ex()
        {
            this.Refresh();
        }
        public bool ClickEx(string downkeys)
        {
            return To0(downkeys);
        }
        public bool To0(string downKeys)
        {
            if (string.IsNullOrEmpty(downKeys) || string.IsNullOrEmpty(keystr))
            {
                return false;
            }
            if (downKeys.Contains(keystr))
            {
                LastTime = DateTime.Now;
                return true;
            }
            return false;
        }
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }

        }

        public bool IsShow { get; set; }
    }
}
